<?php
namespace Activity\Controller;
use Common\Controller\CommonController;

/**
 * 活动模块
 */
class ActivityController extends CommonController {
	protected $_activity;
	protected $_activity_reply;
	protected $_activity_member;
	
	protected function _initialize(){
		parent::_initialize();
		$this->_mycache = S(C('REDIS_DSN'));
		
		$this->_moduleid = 113;
		$this->_activity = new \Activity\Model\ActivityModel();
		$this->_activity_reply = new \Activity\Model\ActivityReplyModel();
		$this->_activity_member = new \Activity\Model\ActivityMemberModel();
		$this->_favorite = new \Common\Model\FavoriteModel();
	}
    
    
	
	
	/**
	 * 我的发布
	 */
	public function myIssuanceAction(){
		$this->_auth();
		$pageIndex = (int)I('request.pageIndex');
		$pageSize = (int)I('request.pageSize');
		$this->_myIssuance($pageIndex , $pageSize);
	}
	
	private function _myIssuance($pageIndex=0 , $pageSize=0){
		$data = array();
		if($pageIndex)$pageIndex--;
		$data['pageIndex'] = $pageIndex;
		$data['pageSize'] = $pageSize;
		$data['custid'] = $this->_custInfo['custid'];
		$result = $this->_activity->getActivityList($data);
		if(!empty($result['data'])){
			foreach($result['data'] as &$activity){
				$activity['nickname'] = $this->_custInfo['nickname'];
				if(!empty($activity['cover'])) $activity['cover'] = json_decode($activity['cover'] , true);
			}
		}else{
			$result['data'] = array();
		}
		
		$this->data = $result['data'];
		$this->outPut();
	}
	
	/**
	 * 我的报名
	 */
	public function mySignupAction(){
		$this->_auth();
		$pageIndex = (int)I('request.pageIndex');
		$pageSize = (int)I('request.pageSize');
		$this->_mySignup($pageIndex , $pageSize);
	}
	
	private function _mySignup($pageIndex=0 , $pageSize=0){
		$data = array();
		if($pageIndex)$pageIndex--;
		$data['pageIndex'] = $pageIndex;
		$data['pageSize'] = $pageSize;
		$data['custid'] = $this->_custInfo['custid'];
		$result = $this->_activity->getJoinList($data);
		if(!empty($result['data'])){
			foreach($result['data'] as &$activity){
				$custInfo = $this->_getCustInfo($activity['custid']);
				$activity['nickname'] = $custInfo['nickname'];
				if(!empty($activity['cover'])) $activity['cover'] = json_decode($activity['cover'] , true);
			}
		}else{
			$result['data'] = array();
		}
		
		$this->data = $result['data'];
		$this->outPut();
	}
	
	
	
    
    /**
     * 发布活动
     */
	public function addActivityAction(){
		$this->_auth();
		$title = I('request.title');
		$starttime = I('request.starttime');
		$endtime = I('request.endtime');
		$personnum = (int)I('request.personnum');
		$address = I('request.address');
		$fee = I('request.fee');
		$description = I('request.description');
		$cover = I('request.cover');
    	$this->_addActivity($title , $personnum , $starttime , $endtime , $address , $fee , $description , $cover);
	}
	
	private function _addActivity($title , $personnum , $starttime=null , $endtime=null , $address="" , $fee=0.00 , $description="" , $cover = ""){
		if(empty($title) || empty($starttime) || empty($personnum)){
			$this->status = 1000;
			$this->msg = L('STATUS_1000');
			$this->outPut();
		}
		
		$data = array();
		$data['custid'] = $this->_custInfo['custid'];
		$data['title'] = $title;
		$data['starttime'] = empty($starttime)?date("Y-m-d H:i:s"):$starttime;
		$data['endtime'] = empty($endtime)?"0000-00-00 00:00:00":$endtime;
		$data['personnum'] = $personnum;
		$data['address'] = $address;
		$data['fee'] = $fee;
		$data['description'] = $description;
		$data['cover'] = $cover;
		$activityid = $this->_activity->addActivity($data);
		if($activityid){
			$friendList = $this->_getFriendList();
			if(!empty($friendList)){//对好友进行提示
				foreach($friendList as &$friend){
					$friendCust[] = $friend['custid'];
				}
			
				//小红点提示好友有新的活动发布
				$msg['from_cust_name'] = $this->_custInfo['nickname'];
				$msg['from_cust_id'] = (int)$this->_custInfo['custid'];
				$msg['type'] = 45;
				$msg['content'] = "";
				//\im::sendMsg(3, $friendCust, array(), $msg , 1);
			
			}
			
			$this->outPut();
		}else{
			$this->status = 2701;
			$this->msg = L('STATUS_2701');
			$this->outPut();
		}
	}
	
	
	
	/**
	 * 修改活动
	 */
	public function updateActivityAction(){
		$this->_auth();
		$activityid = (int)I('request.activityid');
		$title = I('request.title');
		$starttime = I('request.starttime');
		$endtime = I('request.endtime');
		$personnum = (int)I('request.personnum');
		$address = I('request.address');
		$fee = I('request.fee');
		$description = I('request.description');
		$cover = I('request.cover');
		$this->_updateActivity($activityid , $title , $personnum , $starttime , $endtime , $address , $fee , $description , $cover);
	}
	
	private function _updateActivity($activityid , $title , $personnum , $starttime="0000-00-00 00:00:00" , $endtime="0000-00-00 00:00:00" , $address="" , $fee=0.00 , $description="" , $cover = ""){
		if(empty($activityid) || empty($title) || empty($starttime) || empty($personnum)){
			$this->status = 1000;
			$this->msg = L('STATUS_1000');
			$this->outPut();
		}
	
		$data = array();
		$data['custid'] = $this->_custInfo['custid'];
		$data['activityid'] = $activityid;
		$data['title'] = $title;
		$data['starttime'] = $starttime;
		$data['endtime'] = $endtime;
		$data['personnum'] = $personnum;
		$data['address'] = $address;
		$data['fee'] = $fee;
		$data['description'] = $description;
		$data['cover'] = $cover;
		$ret = $this->_activity->updateActivity($data);
		if($ret){
			$this->outPut();
		}else{
			$this->status = 2702;
			$this->msg = L('STATUS_2702');
			$this->outPut();
		}
	}
	
	
	/**
	 * 打开或关闭活动
	 */
	public function switchActivityAction(){
		$this->_auth();
		$activityid = (int)I('request.activityid');
		$status = (int)I('request.status');
		$this->_switchActivity($activityid , $status);
	}
	
	
	private function _switchActivity($activityid , $status){
		if(empty($activityid)){
			$this->status = 1000;
			$this->msg = L('STATUS_1000');
			$this->outPut();
		}
		
		$status = $status > 0 ? 1 : 0;
	
		$data = array();
		$data['custid'] = $this->_custInfo['custid'];
		$data['activityid'] = $activityid;
		$data['status'] = $status;
		$ret = $this->_activity->updateActivity($data);
		if($ret){
			$this->outPut();
		}else{
			$this->status = 2703;
			$this->msg = L('STATUS_2703');
			$this->outPut();
		}
	}

	
	
	protected function _getReplyByActivityId($activityid){
		$activityReplyKey = \cachekey::$key['BASE_KEY_CONF']['ACTIVITY_REPLY'].$activityid;
		$replyInfo = $this->_mycache->get($activityReplyKey);
		if(empty($replyInfo)){
			$replyInfo = $this->_activity_reply->getReplyByActivityId($activityid);
			
			if(!empty($replyInfo)){
				foreach($replyInfo as &$reply){
					$custInfo = $this->_getCustInfo($reply['custid']);
					$reply['nickname'] = $custInfo['nickname'];
						
					if(!empty($reply['pcustid'])){
						$custInfo = $this->_getCustInfo($reply['pcustid']);
						$reply['pnickname'] = $custInfo['nickname'];
					}else{
						$reply['pnickname'] = "";
					}
				}
				
				$this->_mycache->set($activityReplyKey , $replyInfo);
		    	$this->_mycache->expire($activityReplyKey , C('ACTIVITY_REPLY_EXPIRE'));
			}else{
				$replyInfo = array();
			}
		}
		
		$returnData = array('replylist'=>array() , 'praiselist'=>array());
		if(!empty($replyInfo)){
			foreach($replyInfo as &$reply){
				if($reply['type'] == "1"){
					$returnData['replylist'][] = array('replyid'=>$reply['replyid'],'custid'=>$reply['custid'],'nickname'=>$reply['nickname'],'pcustid'=>$reply['pcustid'],'pnickname'=>$reply['pnickname'],'content'=>$reply['content'],'createtime'=>$reply['createtime']);
				}elseif($reply['type'] == "2"){
					$returnData['praiselist'][] = array('custid'=>$reply['custid'],'nickname'=>$reply['nickname']);
				}
			}
		}
		
		return $returnData;
	}
	
	
	/**
	 * 活动列表
	 */
	public function getActivityListAction(){
		$custid = (int)I('request.custid');
		$pageIndex = (int)I('request.pageIndex');
		$pageSize = (int)I('request.pageSize');
		$this->_getActivityList($custid , $pageIndex , $pageSize);
	}
	
	private function _getActivityList($custid = 0 , $pageIndex = 0 , $pageSize = 0){
		$data = array();
		if($pageIndex)$pageIndex--;
		$data['pageIndex'] = $pageIndex;
		$data['pageSize'] = $pageSize;
		if(!empty($custid)){
			$data['custid'] = $custid;
			$custInfo = $this->_getCustInfo($custid);
			$nickname = $custInfo['nickname'];
			$signature = $custInfo['signature'];
		}
		$result = $this->_activity->getActivityList($data);
		if(!empty($result['data'])){
			foreach($result['data'] as &$activity){
				if(!empty($custid)){
					$activity['nickname'] = $nickname;
					$activity['signature'] = $signature;
				}else{
					$custInfo = $this->_getCustInfo($activity['custid']);
					$activity['nickname'] = $custInfo['nickname'];
					$activity['signature'] = $custInfo['signature'];
				}
				
				if(!empty($activity['cover'])) $activity['cover'] = json_decode($activity['cover'] , true);
			}
		}else{
			$result['data'] = array();
		}
		
		$this->data = $result;
		$this->outPut();
	}
	
	
	/**
	 * 活动详情
	 */
	public function getActivityDetailAction(){
		$activityid = (int)I('request.activityid');
		$this->_getActivityDetail($activityid);
	}
	
	private function _getActivityDetail($activityid){
		if(empty($activityid)){
    		$this->status = 1000;
    		$this->msg = L('STATUS_1000');
    		$this->outPut();
		}
	
		$activity = $this->_activity->getActivityById($activityid);
		if(empty($activity)){
			$this->status = 2707;
			$this->msg = L('STATUS_2707');
			$this->outPut();
		}
		
		
		$custInfo = $this->_getCustInfo($activity['custid']);
		$activity['nickname'] = $custInfo['nickname'];
		$activity['area'] = $custInfo['area'];
		$activity['signature'] = $custInfo['signature'];

		if(!empty($activity['cover'])) $activity['cover'] = json_decode($activity['cover'] , true);

		$replyData = $this->_getReplyByActivityId($activity['activityid']);
		
		//回复列表
		$activity['replylist'] = $replyData['replylist'];
		
		//赞的列表
		$activity['praiselist'] = $replyData['praiselist'];
		$activity['_praise'] = "0";
		if(!empty($activity['praiselist'])){
			foreach($activity['praiselist'] as &$praise){
				if($praise['custid'] == $this->_custInfo['custid'])$activity['_praise'] = "1";
			}
		}
		
		$data = array();
		$data['custid'] = $this->_custInfo['custid'];
		$data['activityid'] = $activityid;
		$isjoin = $this->_activity_member->checkMember($data);//检查是否已经参加
		$activity['_isjoin'] = (string)$isjoin;
		
		
		$memberCount = $this->_activity_member->countMember($activityid);
		$activity['memberCount'] = (string)$memberCount;
		
		$data = array();
		$data['custid'] = $this->_custInfo['custid'];
		$data['targetid'] = $activityid;
		$data['moduleid'] = $this->_moduleid;
		$isfavorited = $this->_favorite->checkFavorite($data);
		$activity['_isfavorited'] = (string)$isfavorited;
		
		$this->data = $activity;
		$this->outPut();
	}
	
	
	/**
	 * 删除活动
	 */
	public function deleteActivityAction(){
		$this->_auth();
		$activityid = (int)I('request.activityid');
		$this->_deleteActivity($activityid);
	}
	
	
	private function _deleteActivity($activityid){
		if(empty($activityid)){
    		$this->status = 1000;
    		$this->msg = L('STATUS_1000');
    		$this->outPut();
		}
		
		$data = array();
		$data['activityid'] = $activityid;
		$data['custid'] = $this->_custInfo['custid'];
		$ret = $this->_activity->deleteActivity($data);
		if($ret){
			$activityReplyKey = \cachekey::$key['BASE_KEY_CONF']['ACTIVITY_REPLY'].$activityid;
			$this->_mycache->rm($activityReplyKey);
			
			$this->outPut();
		}else{
			$this->status = 2715;
			$this->msg = L('STATUS_2715');
			$this->outPut();
		}
	}
	
	
	/**
	 * 参加活动
	 */
	public function joinActivityAction(){
		$this->_auth();
		$activityid = (int)I('request.activityid');
		$this->_joinActivity($activityid);
	}
	
	
	private function _joinActivity($activityid){
		if(empty($activityid)){
			$this->status = 1000;
			$this->msg = L('STATUS_1000');
			$this->outPut();
		}
		
		$activity = $this->_activity->getActivityById($activityid);
		if(empty($activity)){
			$this->status = 2707;
			$this->msg = L('STATUS_2707');
			$this->outPut();
		}
		
		
		/* $applyKey = \cachekey::$key['BASE_KEY_CONF']['APPLY_ACTIVITY_TIME'].$this->_custInfo['custid'].":".$activity['custid'];
		$timelast = $this->_mycache->get($applyKey);
		if(__ENV__ == "production"){
			if(time() - (int)$timelast > (int)C('DENY_APPLY_ACTIVITY_EXPIRE')){
				$this->_mycache->set($applyKey , time());
				$this->_mycache->expire($applyKey , C('DENY_APPLY_ACTIVITY_EXPIRE'));
			}
		}
		
		//发推送消息
		$content = L('JOIN_ACTIVITY_NOTICE',array('title' => $activityid['title']));
		$noticeid = $this->_joinActivityNotice($activity['activityid'] , $activity['custid'] , $this->_custInfo , $content);
		if($noticeid){
			$this->outPut();
		}else{
			$this->status = 2715;
			$this->msg = L('STATUS_2715');
			$this->outPut();
		} */
		
		
		$data = array();
		$data['activityid'] = $activityid;
		$data['custid'] = $this->_custInfo['custid'];
		$ret = $this->_activity_member->addMember($data);
		if($ret){
			$this->outPut();
		}else{
			$this->status = 2715;
			$this->msg = L('STATUS_2715');
			$this->outPut();
		}
		
	}
	
	private function _joinActivityNotice($activityid , $tcustid , $custInfo , $content){
		$noticeData = array();
		$noticeData['content'] = $content;
		$noticeData['custid'] = $custInfo['custid'];
		$noticeData['tcustid'] = $tcustid;
		$noticeData['moduleid'] = $this->_moduleid;
		$noticeData['businessid'] = $activityid;
		$noticeid = $this->_createNotice($noticeData);
		
		$msg['from_cust_name'] = $custInfo['nickname'];
		$msg['from_cust_id'] = (int)$custInfo['custid'];
		$msg['type'] = 12;
		$msg['content'] = $content;
		$msg['notice_id'] = (string)$noticeid;
		//\im::sendMsg(3, array($tcustid), array(), $msg , 1);
		
		return $noticeid;
	}
	
	
	/**
	 * 审核活动成员
	 */
	public function aceptActivityMemberAction(){
		$this->_auth();
		$noticeid = (int)I('request.noticeid');
		$accept = (int)I('request.accept');
		$this->_aceptActivityMember($noticeid , $accept);
	}
	
	
	private function _aceptActivityMember($noticeid , $accept = 1){
		if(empty($noticeid)){
    		$this->status = 1000;
    		$this->msg = L('STATUS_1000');
    		$this->outPut();
    	}
    	
    	//check notice
    	$noticeInfo = $this->_getNotice(array('noticeid'=>$noticeid));
    	if(!empty($noticeInfo)){
    		if($this->_custInfo['custid'] != $noticeInfo['tcustid']){
    			$this->status = 1001;
    			$this->msg = L('STATUS_1001');
    			$this->outPut();
    		}
    		$custid = $noticeInfo['custid'];
    		$activityid = $noticeInfo['businessid'];
    	}else{
    		$this->status = 1001;
    		$this->msg = L('STATUS_1001');
    		$this->outPut();
    	}

    	if(empty($accept)){//拒绝
    		$this->outPut();
    	}

    		
		$data = array();
		$data['custid'] = $custid;
		$data['activityid'] = $activityid;
		$data['status'] = 1;
		$ret = $this->_activity_member->addMember($data);//加为成员
		if($ret){
			$this->_activity->acceptInc($activityid);
			
			//发推送消息
			$content = L('ACCEPT_ACTIVITY_NOTICE');
			$this->_acceptMemberNotice($custid , $this->_custInfo , $noticeid , $content);
			
			$custInfo = $this->_getCustInfo($custid);
			$this->data = $custInfo;
			$this->outPut();
		}else{
			$this->status = 2704;
			$this->msg = L('STATUS_2704');
			$this->outPut();
		}
    }
    
    
    private function _acceptMemberNotice($tcustid , $custInfo , $noticeid , $content){
    	$noticeData = array();
    	$noticeData['content'] = $content;
    	$noticeData['custid'] = $custInfo['custid'];
    	$noticeData['tcustid'] = $tcustid;
    	$noticeData['returnid'] = $noticeid;
    	$noticeData['moduleid'] = $this->_moduleid;
    	$noticeData['status'] = 2;
    	$this->_createNotice($noticeData);
    	$this->_updateNotice(array('noticeid'=>$noticeid , 'tcustid'=>$custInfo['custid'] , 'status'=>2));
    	
    	$msg['from_cust_name'] = $custInfo['nickname'];
    	$msg['from_cust_id'] = (int)$custInfo['custid'];
    	$msg['type'] = 13;
    	$msg['content'] = $content;
    	$msg['notice_id'] = (string)$noticeid;
    	$msg['data'] = $custInfo;
    	//\im::sendMsg(3, array($tcustid), array(), $msg , 1);
    }
	
	/**
	 * 赞或取消
	 */
	public function praiseAction(){
		$this->_auth();
		$activityid = (int)I('request.activityid');
		$op = (int)I('request.op');
		$this->_praise($activityid , $op);
	}
	
	private function _praise($activityid , $op = 1){
		if(empty($activityid)){
    		$this->status = 1000;
    		$this->msg = L('STATUS_1000');
    		$this->outPut();
		}
		
		$activityInfo = $this->_activity->getActivityById($activityid);
		
		$data = array();
		$data['activityid'] = $activityid;
		$data['custid'] = $this->_custInfo['custid'];
		$data['type'] = 2;
		$data['status'] = $op == 1 ? $op : 0;
		if($op){
			$ret = $this->_activity_reply->addPraise($data);
			if($ret && $activityInfo['custid'] != $this->_custInfo['custid']){//对好友进行提示
				//与custid相关的赞
				$relationActivityReplyKey = \cachekey::$key['BASE_KEY_CONF']['CUST_RELATION_ACTIVITY_REPLY'].$activityInfo['custid'];
				$this->_mycache->sadd($relationActivityReplyKey , $ret);
				
				$relationActivityPraiseKey = \cachekey::$key['BASE_KEY_CONF']['CUST_ACTIVITY_PRAISE'].$this->_custInfo['custid'].":".$activityInfo['custid'];
				$praised = $this->_mycache->get($relationActivityPraiseKey);
				if(empty($praised)){//从未赞过就发系统消息
					$this->_mycache->set($relationActivityPraiseKey , $ret);
					//小红点提示好友有新的相关活动回复或赞
					$msg['from_cust_name'] = $this->_custInfo['nickname'];
					$msg['from_cust_id'] = (int)$this->_custInfo['custid'];
					$msg['type'] = 46;
					$msg['content'] = "";
					//\im::sendMsg(3, array($activityInfo['custid']), array(), $msg , 1);
				}
			}
		}else{
			$ret = $this->_activity_reply->deletePraise($data);
		}
		
		
		if($ret){
			//删除活动赞列表缓存
			$activityReplyKey = \cachekey::$key['BASE_KEY_CONF']['ACTIVITY_REPLY'].$activityid;
			$this->_mycache->rm($activityReplyKey);
			
			$this->outPut();
		}else{
			$this->status = 2709;
			$this->msg = L('STATUS_2709');
			$this->outPut();
		}
		
		
	}
	
	
	
	/**
	 * 发表回复
	 */
	public function addReplyAction(){
		$this->_auth();
		$activityid = (int)I('request.activityid');
		$content = I('request.content');
		$pcustid = (int)I('request.pcustid');
		$this->_addReply($activityid , $content , $pcustid);
	}
	
	private function _addReply($activityid , $content , $pcustid=0){
		if(empty($activityid) || empty($content)){
    		$this->status = 1000;
    		$this->msg = L('STATUS_1000');
    		$this->outPut();
		}
		
		$data = array();
		$data['activityid'] = $activityid;
		$data['custid'] = $this->_custInfo['custid'];
		$data['content'] = $content;
		$data['pcustid'] = $pcustid;
		$data['type'] = 1;
		$ret = $this->_activity_reply->addReply($data);
		if($ret){
			//删除活动回复列表缓存
			$activityReplyKey = \cachekey::$key['BASE_KEY_CONF']['ACTIVITY_REPLY'].$activityid;
			$this->_mycache->rm($activityReplyKey);
			
			$activityInfo = $this->_activity->getActivityById($activityid);
			if(!empty($activityInfo) && $activityInfo['custid'] != $this->_custInfo['custid']){//对好友进行提示
				//与custid相关的回复
				$relationActivityReplyKey = \cachekey::$key['BASE_KEY_CONF']['CUST_RELATION_ACTIVITY_REPLY'].$activityInfo['custid'];
				$this->_mycache->sadd($relationActivityReplyKey , $ret);
				
				//小红点提示好友有新的相关活动回复或赞
				$msg['from_cust_name'] = $this->_custInfo['nickname'];
				$msg['from_cust_id'] = (int)$this->_custInfo['custid'];
				$msg['type'] = 46;
				$msg['content'] = "";
				//\im::sendMsg(3, array($activityInfo['custid']), array(), $msg , 1);
			}
			
			if($pcustid && $pcustid != $this->_custInfo['custid']){//对好友进行提示
				//与pcustid相关的回复
				$relationActivityReplyKey = \cachekey::$key['BASE_KEY_CONF']['CUST_RELATION_ACTIVITY_REPLY'].$pcustid;
				$this->_mycache->sadd($relationActivityReplyKey , $ret);
				
				//小红点提示好友有新的相关活动回复或赞
				$msg['from_cust_name'] = $this->_custInfo['nickname'];
				$msg['from_cust_id'] = (int)$this->_custInfo['custid'];
				$msg['type'] = 46;
				$msg['content'] = "";
				//\im::sendMsg(3, array($pcustid), array(), $msg , 1);
			}
			
			$replyInfo = $this->_activity_reply->getReplyById($ret);
			$custInfo = $this->_getCustInfo($replyInfo['custid']);
			$replyInfo['nickname'] = $custInfo['nickname'];
			
			if(!empty($replyInfo['pcustid'])){
				$custInfo = $this->_getCustInfo($replyInfo['pcustid']);
				$replyInfo['pnickname'] = $custInfo['nickname'];
			}else{
				$replyInfo['pnickname'] = "";
			}
			
			$this->data = $replyInfo;
			$this->outPut();
		}else{
			$this->status = 2711;
			$this->msg = L('STATUS_2711');
			$this->outPut();
		}
	}
	
	
	/**
	 * 删除回复
	 */
	public function deleteReplyAction(){
		$this->_auth();
		$replyid = (int)I('request.replyid');
		$this->_deleteReply($replyid);
	}
	
	private function _deleteReply($replyid){
		if(empty($replyid)){
    		$this->status = 1000;
    		$this->msg = L('STATUS_1000');
    		$this->outPut();
		}
		
		$replyInfo = $this->_activity_reply->getReplyById($replyid);
		if(!empty($replyInfo)){
			$activityid = $replyInfo['activityid'];
		}else{
			$this->status = 2713;
			$this->msg = L('STATUS_2713');
			$this->outPut();
		}
		
		$data = array();
		$data['custid'] = $this->_custInfo['custid'];
		$data['replyid'] = $replyid;
		$ret = $this->_activity_reply->deleteReply($data);
		if($ret){
			//删除活动回复列表缓存
			$activityReplyKey = \cachekey::$key['BASE_KEY_CONF']['ACTIVITY_REPLY'].$activityid;
			$this->_mycache->rm($activityReplyKey);
			
			$this->outPut();
		}else{
			$this->status = 2713;
			$this->msg = L('STATUS_2713');
			$this->outPut();
		}
	} 
	
		
	
	/**
	 * 设置封面图
	 */
	public function setCoverAction(){
		$this->_auth();
		$activityid = (int)I('request.activityid');
		$this->_setCover($activityid);
	}
	
	private function _setCover($activityid){
		$ret = $this->_sendFile(false);
		if(empty($ret)){
			$this->status = 2717;
			$this->msg = L('STATUS_2717');
			$this->outPut();
		}
		
		$cover = array('fileid'=>$ret['fileid'] , 'filehash'=>$ret['filehash']);
		$activityInfo['activityid'] = $activityid;
		$activityInfo['cover'] = json_encode($cover);
		
		$ret = $this->_activity->updateActivity($activityInfo);
		if($ret){
			$returnData['activityid'] = $activityid;
			$returnData['cover'] = $cover;
			$this->data = $returnData;
			$this->outPut();
		}else{
			$this->status = 2717;
			$this->msg = L('STATUS_2717');
			$this->outPut();
		}
	}
	

	
	/**
	 * 发送文件
	 */
	public function sendFileAction(){
		$this->_auth();
		$this->_sendFile();
	}
	
	
	/**
	 * 报名列表
	 */
	public function getMemberListAction(){
		$activityid = (int)I('request.activityid');
		$pageIndex = (int)I('request.pageIndex');
		$pageSize = (int)I('request.pageSize');
		$this->_getMemberList($activityid , $pageIndex , $pageSize);
	}
	
	private function _getMemberList($activityid , $pageIndex , $pageSize){
		$data = array();
		if($pageIndex)$pageIndex--;
		$data['pageIndex'] = $pageIndex;
		$data['pageSize'] = $pageSize;
		$data['activityid'] = $activityid;
		$memberList = $this->_activity_member->getMemberList($data);
		if(!empty($memberList['data'])) {
			foreach($memberList['data'] as &$cust){
				$cust['checkintime'] = ($cust['checkintime']=="0000-00-00 00:00:00") ? "" : $cust['checkintime'];
				$custInfo = $this->_getCustInfo($cust['custid']);
				$cust['area'] = $custInfo['area'];
				$cust['nickname'] = $custInfo['nickname'];
				$cust['sex'] = $custInfo['sex'];
				$cust['signature'] = $custInfo['signature'];
			}
			
			$this->data = $memberList['data'];
		}
		$this->outPut();
	}
	
	
	
	/**
	 * 获取活动二维码
	 */
	public function getQRCodeAction(){
		$this->_auth();
		$activityid = (int)I('request.activityid');
		$this->_getQRCode($activityid);
	}
	
	private function _getQRCode($activityid){
		$activityInfo = $this->_activity->getActivityById($activityid);
		if($activityInfo['custid'] != $this->_custInfo['custid']){
    		$this->status = 1001;
    		$this->msg = L('STATUS_1001');
    		$this->outPut();
		}
	
		$data['activityid'] = $activityInfo['activityid'];
		$data['custid'] = $activityInfo['custid'];
		$data['title'] = $activityInfo['title'];
		$data['personnum'] = $activityInfo['personnum'];
		$data['fee'] = $activityInfo['fee'];
		$data['starttime'] = $activityInfo['starttime'];
		$data['endtime'] = $activityInfo['endtime'];
    	$data['controller'] = CONTROLLER_NAME;
    	//$data = CONTROLLER_NAME . "://" . json_encode($data);
    	$data = C('QR_CODE_INFO_URL') . "?data=" . \aes::encrypt(json_encode($data) , C('TOKEN_SALT'));
			
		$qrcodefile = "/tmp/activity_{$activityInfo['activityid']}.png";
		$logofile = "";//准备好的活动avatar图片地址或图片url
			
		\tools::makeQrcode($data , $qrcodefile , $logofile);
	}
	
	
	
	/**
	 * 签到
	 */
	public function checkinAction(){
		$this->_auth();
		$activityid = (int)I('request.activityid');
		$this->_checkin($activityid);
	}
	
	
	private function _checkin($activityid){
		$data['activityid'] = $activityid;
		$data['custid'] = $this->_custInfo['custid'];
		$data['checkintime'] = date("Y-m-d H:i:s");
		$ret = $this->_activity_member->updateMember($data);
		if($ret){
			$this->outPut();
		}else{
			$this->status = 2718;
			$this->msg = L('STATUS_2718');
			$this->outPut();
		}
	}
	
	
}
